/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tracksuite.integration;

import java.util.List;
import javax.persistence.Query;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Example;
import tracksuite.exception.DAOException;

/**
 *
 * @author mfloriano
 */
public class CRUDDAO<TO extends GenericTO> extends HibernateSupport {
 /*
     * @see com.imagem.emprel.integration.dao.DAO#startOperation()
     */
    public void startOperation() {
        startHibernateOperation();
    }

    /*
     * @see com.imagem.emprel.integration.dao.DAO#stopOperation(boolean)
     */
    public void stopOperation(boolean commitChanges) {
        stopHibernateOperation(commitChanges);
    }

    /*
     * @see com.imagem.emprel.integration.dao.DAO#save(java.lang.Object)
     */
    public void save(TO instance) {
        getEntityManager().persist(instance);
    }

    /*
     * @see com.imagem.emprel.integration.dao.DAO#merge(java.lang.Object)
     */
    public TO merge(TO instance) throws DAOException {
        instance = getEntityManager().merge(instance);
        return instance;
    }

    /*
     * @see com.imagem.emprel.integration.dao.DAO#remove(java.lang.Object)
     */
    public void remove(TO instance) {
        getEntityManager().remove(instance);
    }

    /*
     * @see com.imagem.emprel.integration.dao.DAO#find(java.lang.String)
     */
    public List<TO> find(String queryString) throws DAOException {
        List<TO> results = null;
        try {
            Query query = getEntityManager().createQuery(queryString);
            results = query.getResultList();
        } catch (RuntimeException re) {
            re.printStackTrace();
            throw new DAOException("Problemas na localização dos objetos", re);
        }
        return results;
    }

    /*
     * @see com.imagem.emprel.integration.dao.DAO#find(java.lang.String,
     *      java.lang.Integer, java.lang.Integer)
     */
    public List<TO> find(String queryString, Integer begin, Integer end) throws DAOException {
        List<TO> results = null;
        try {
            Query query = getEntityManager().createQuery(queryString);
            query.setFirstResult(begin);
            query.setMaxResults(end);
            results = query.getResultList();
        } catch (RuntimeException re) {
            re.printStackTrace();
            throw new DAOException("Problemas na localização dos objetos", re);
        }
        return results;
    }

    /*
     * @see com.imagem.emprel.integration.dao.DAO#findById(java.lang.Integer)
     */
    public TO findById(Class<TO> classType, Object id) throws DAOException {
        TO instance = null;
        try {
            instance = getEntityManager().find(classType, id);
        } catch (RuntimeException re) {
            re.printStackTrace();
            throw new DAOException("Problemas na localização dos objetos", re);
        }
        return instance;
    }

    /*
     * @see com.imagem.emprel.integration.dao.DAO#findByExample(java.lang.Object)
     */
    public List<TO> findByExample(TO instance) throws DAOException {
        Session session = (Session) getEntityManager().getDelegate();
        Example example = Example.create(instance).excludeZeroes();
        Criteria criteria = session.createCriteria(instance.getClass()).add(example);
        return criteria.list();
    }

    /*
     * @see com.imagem.emprel.integration.dao.DAO#findByExample(java.lang.Object,
     *      java.lang.Integer, java.lang.Integer)
     */
    public List<TO> findByExample(TO instance, Integer begin, Integer end) throws DAOException {
        Session session = (Session) getEntityManager().getDelegate();
        Example example = Example.create(instance).excludeZeroes();
        Criteria criteria = session.createCriteria(instance.getClass()).add(example);
        criteria.setFirstResult(begin);
        criteria.setMaxResults(end);
        return criteria.list();
    }

    /*
     * @see com.imagem.emprel.integration.dao.DAO#findAll()
     */
    public List<TO> findAll(Class<TO> classType) throws DAOException {
        List<TO> results = null;
        try {
            Query query = getEntityManager().createQuery("SELECT object(o) FROM " + classType.getName() + " AS o");
            results = query.getResultList();
        } catch (RuntimeException re) {
            re.printStackTrace();
            throw new DAOException("Problemas na localização dos objetos", re);
        }
        return results;
    }

    /*
     * @see com.imagem.emprel.integration.dao.DAO#findAll(java.lang.Integer,
     *      java.lang.Integer)
     */
    public List<TO> findAll(Class<TO> classType, Integer begin, Integer end) throws DAOException {
        List<TO> results = null;
        try {
            Query query = getEntityManager().createQuery("SELECT object(o) FROM " + classType.getName() + " AS o");
            query.setFirstResult(begin);
            query.setMaxResults(end);
            results = query.getResultList();
        } catch (RuntimeException re) {
            re.printStackTrace();
            throw new DAOException("Problemas na localização dos objetos", re);
        }
        return results;
    }
}
